package com.simplefaith.delisty;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

import com.simplefaith.delisty.client.ClientInstance;
import com.simplefaith.delisty.client.model.PacketWork;
import com.simplefaith.delisty.client.packet.LoginPacket;
import com.simplefaith.delisty.util.ActivityStackControlUtil;

public class LoginActivity extends Activity {

	private Button loginButton;

	private Handler handler;
	private Runnable switchActivityRun;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedBundleInstance) {

		//
		super.onCreate(savedBundleInstance);
		ActivityStackControlUtil.add(this);
		// hide title
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// set view
		setContentView(R.layout.activity_login);

		ClientInstance.getClient();

		handler = new Handler();

		switchActivityRun = new Runnable() {

			@Override
			public void run() {

				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);

				LoginActivity.this.startActivity(intent);

				progressDialog.dismiss();

			}
		};

		loginButton = (Button) findViewById(R.id.buttonLogin);

		loginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				progressDialog = ProgressDialog.show(LoginActivity.this,
						"Login...", "please wait...");

				PacketWork work = new PacketWork() {

					@Override
					public void packetAccept() {
						System.out.println("receive login");

						handler.post(switchActivityRun);
					}

					@Override
					public void packetReject() {

					}
				};

				new LoginPacket("account", "password", work);

			}
		});

	}

	protected void onDestroy() {
		super.onDestroy();
		ActivityStackControlUtil.remove(this);
	}

	@Override
	public void onBackPressed() {
		ActivityStackControlUtil.finishProgram();
	}
}
